import { danmaku } from './danmaku';
import { assSubtitle } from './assSubtitle';
import { danmakuParser } from './danmakuParser';
import { danmakuType } from './models/danmakuType';

export class danmakuConverter {
  static convertToAss(danmakus: danmaku[], videoWidth: number, videoHeight: number, fontName: string = "Microsoft YaHei", fontSize: number = 64, lineCount: number = 14, bottomMargin: number = 180, shift: number = 0): string {
    let header = `
    [Script Info]
    ScriptType: v4.00+
    Collisions: Normal
    PlayResX: ${videoWidth}
    PlayResY: ${videoHeight}

    [V4+ Styles]
    Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
    Style: AcplayDefault, ${fontName}, ${fontSize}, &H00FFFFFF, &H00FFFFFF, &H00000000, &H00000000, 0, 0, 0, 0, 100, 100, 0.00, 0.00, 1, 1, 0, 2, 20, 20, 20, 0

    [Events]
    Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text`;
    let arr = danmakus.filter((d) => d.type == danmakuType.normal || d.type == danmakuType.normal2 || d.type == danmakuType.normal3 || d.type == danmakuType.top || d.type == danmakuType.bottom || d.type == danmakuType.reverse);
    let dic1 = new Map<number, number>(), dic2 = new Map<number, number>();
    let data: assSubtitle[] = [];
    for (let item of arr)
      data.push(new assSubtitle(item, dic1, dic2, videoWidth, videoHeight, fontSize, lineCount, bottomMargin, shift));
    return header;
  }

  static convertToAssFromXml(danmakusXml: string, videoWidth: number, videoHeight: number, fontName: string = "Microsoft YaHei", fontSize: number = 64, lineCount: number = 14, bottomMargin: number = 180, shift: number = 0): string {
    var danmakus = danmakuParser.loadXmlFromString(danmakusXml);
    return this.convertToAss(danmakus, videoWidth, videoHeight, fontName, fontSize, lineCount, bottomMargin, shift);
  }

  static convertToAssFromFile(path: string, videoWidth: number, videoHeight: number, fontName: string = "Microsoft YaHei", fontSize: number = 64, lineCount: number = 14, bottomMargin: number = 180, shift: number = 0): string {
    var danmakus = danmakuParser.loadXmlFromFile(path);
    return this.convertToAss(danmakus, videoWidth, videoHeight, fontName, fontSize, lineCount, bottomMargin, shift);
  }
}